﻿//-----------------------------------------------------------------------
// <copyright file="EditCustomerFromAdminViewModel.cs" company="FER">
//     Copyright (c) All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace NMBP.View.ViewModel
{
    using System;
    using System.Collections.Generic;

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    using NMBP.DAL;
    using NMBP.Models;
    using NMBP.Models.Enums;
    using NMBP.View.Validators;
    using NMBP.ViewModels.Dialogs;

    /// <summary>
    /// This class contains properties that the customer View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class EditCustomerFromAdminViewModel : ViewModelBase
    {
        #region Binding Fields
        /// <summary>
        /// Gets or sets customers name
        /// </summary>
        private string name;

        /// <summary>
        /// Gets or sets customers surname
        /// </summary>
        private string surname;

        /// <summary>
        /// Gets or sets customers username
        /// </summary>
        private string username;

        /// <summary>
        /// Gets or sets customers password
        /// </summary>
        private string password;

        /// <summary>
        /// Gets or sets customers contact
        /// </summary>
        private string contact;

        /// <summary>
        /// Gets or sets customers bank curd number
        /// </summary>
        private string bankCardNumber;

        /// <summary>
        /// Gets or sets selected payment method
        /// </summary>
        private string selectedPaymentMethod;

        /// <summary>
        /// Gets or sets list of available payment methods
        /// </summary>
        private List<string> availablePaymentMethod;

        /// <summary>
        /// Action raised when customer view model needs to be initialized
        /// </summary>
        private Action<Worker> initCustomerViewModel;

        /// <summary>
        /// Gets or sets list of all customers
        /// </summary>
        private List<Customer> allCustomers;

        /// <summary>
        /// Gets or sets selected customer
        /// </summary>
        private Customer selectedCustomer;
        #endregion

        /// <summary>
        /// Gets or sets recieved customer
        /// </summary>
        private Worker recievedWorker;

        /// <summary>
        /// Gets or sets a value indicating whether enabled username
        /// </summary>
        private bool isUsernameEnabled;

        /// <summary>
        /// Initializes a new instance of the EditCustomerViewModel class
        /// </summary>
        public EditCustomerFromAdminViewModel()
        {
            this.initCustomerViewModel = new Action<Worker>(this.InitializeViewModel);
            Messenger.Default.Register<Worker>(this, "InitializeEditCustomerFromAdmin", this.initCustomerViewModel);

            this.SaveEditCustomer = new RelayCommand(
                () =>
                {

                    var newCustomer = new Customer();
                    newCustomer.Name = this.Name;
                    newCustomer.Surname = this.Surname;
                    newCustomer.Username = this.Username;
                    newCustomer.Password = this.Password;
                    newCustomer.Contact = this.Contact;
                    newCustomer.BankCardNumber = this.BankCardNumber;
                    newCustomer.PrefferedPayment = this.DeterminePreferedPayment();

                    if (!CustomerValidator.ValidateCustomer(newCustomer))
                    {
                        this.DialogService.ShowMessage("Neispravno zadani parametri kupca. Ne spremam.", MessageType.ERROR);
                        return;
                    }

                    this.DbFacade.SaveCustomer(newCustomer);

                    Messenger.Default.Send(this.recievedWorker, "CloseEditCustomerFromAdminWindow");
                });

            this.CancelEditCustomer = new RelayCommand(
                () =>
                {
                    Messenger.Default.Send(this.recievedWorker, "CloseEditCustomerFromAdminWindow");
                });
        }

        #region Bindable Properties
        /// <summary>
        /// Gets or sets dbFacade
        /// </summary>
        public IDbFacade DbFacade { get; set; }

        /// <summary>
        /// Gets or sets dialog service
        /// </summary>
        public IDialogService DialogService { get; set; }

        /// <summary>
        /// Gets or sets customers name
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets customers surname
        /// </summary>
        public string Surname
        {
            get
            {
                return this.surname;
            }

            set
            {
                this.surname = value;
                this.RaisePropertyChanged("Surname");
            }
        }

        /// <summary>
        /// Gets or sets customers username
        /// </summary>
        public string Username
        {
            get
            {
                return this.username;
            }

            set
            {
                this.username = value;
                this.RaisePropertyChanged("Username");
            }
        }

        /// <summary>
        /// Gets or sets customers password
        /// </summary>
        public string Password
        {
            get
            {
                return this.password;
            }

            set
            {
                this.password = value;
                this.RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// Gets or sets customers contact
        /// </summary>
        public string Contact
        {
            get
            {
                return this.contact;
            }

            set
            {
                this.contact = value;
                this.RaisePropertyChanged("Contact");
            }
        }

        /// <summary>
        /// Gets or sets customers bank curd number
        /// </summary>
        public string BankCardNumber
        {
            get
            {
                return this.bankCardNumber;
            }

            set
            {
                this.bankCardNumber = value;
                this.RaisePropertyChanged("BankCardNumber");
            }
        }

        /// <summary>
        /// Gets or sets selected payment method
        /// </summary>
        public string SelectedPaymentMethod
        {
            get
            {
                return this.selectedPaymentMethod;
            }

            set
            {
                this.selectedPaymentMethod = value;
                this.RaisePropertyChanged("SelectedPaymentMethod");
            }
        }

        /// <summary>
        /// Gets or sets list of available payment methods
        /// </summary>
        public List<string> AvailablePaymentMethod
        {
            get
            {
                return this.availablePaymentMethod;
            }

            set
            {
                this.availablePaymentMethod = value;
                this.RaisePropertyChanged("AvailablePaymentMethod");
            }
        }

        /// <summary>
        /// Gets or sets selected customer
        /// </summary>
        public Customer SelectedCustomer
        {
            get
            {
                return this.selectedCustomer;
            }

            set
            {
                this.selectedCustomer = value;
                if (value != null)
                {
                    if (value.Username == "-- odaberite --")
                    {
                        this.IsUsernameEnabled = true;
                        this.ClearAllFields();
                    }
                    else
                    {
                        this.IsUsernameEnabled = false;
                        this.LoadAllValues();
                    }
                }
                this.RaisePropertyChanged("SelectedCustomer");
            }
        }

        /// <summary>
        /// Gets or sets list of all customers
        /// </summary>
        public List<Customer> AllCustomers
        {
            get
            {
                return this.allCustomers;
            }

            set
            {
                this.allCustomers = value;
                this.RaisePropertyChanged("AllCustomers");
            }
        }
        #endregion

        #region Bindable commands
        /// <summary>
        /// Gets the command raised when save edit customer button is clicked
        /// </summary>
        public RelayCommand SaveEditCustomer { get; private set; }

        /// <summary>
        /// Gets the command raised when save edit customer button is clicked
        /// </summary>
        public RelayCommand CancelEditCustomer { get; private set; }

        /// <summary>
        /// Gets or sets a value indicating whether enabled username
        /// </summary>
        public bool IsUsernameEnabled
        {
            get
            {
                return this.isUsernameEnabled;
            }

            set
            {
                this.isUsernameEnabled = value;
                this.RaisePropertyChanged("IsUsernameEnabled");
            }
        }

        #endregion

        /// <summary>
        /// Initializes view model
        /// </summary>
        /// <param name="worker">worker to initialize view model</param>
        private void InitializeViewModel(Worker worker)
        {
            if (this.DbFacade == null)
            {
                this.DbFacade = new DbFacade();
            }

            this.recievedWorker = worker;
            this.AvailablePaymentMethod = new List<string>() { "Gotovina", "Kartica" };
            this.SelectedPaymentMethod = this.AvailablePaymentMethod[0];
            this.DialogService = new DialogService();
            this.AllCustomers = this.DbFacade.GetCustomers();
            this.AllCustomers.Insert(0, new Customer{ Username = "-- odaberite --"});
            this.SelectedCustomer = this.AllCustomers[0];

            this.Name = string.Empty;
            this.Surname = string.Empty;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.Contact = string.Empty;
            this.BankCardNumber = string.Empty;
        }

        /// <summary>
        /// Determines selected payment method
        /// </summary>
        /// <returns>Payment method</returns>
        private PaymentMethod DeterminePreferedPayment()
        {
            if (this.SelectedPaymentMethod == "Gotovina")
            {
                return PaymentMethod.Cash;
            }

            return PaymentMethod.Card;
        }

        /// <summary>
        /// Loads values for selected customer
        /// </summary>
        private void LoadAllValues()
        {
            this.Name = this.SelectedCustomer.Name;
            this.Surname = this.SelectedCustomer.Surname;
            this.Username = this.SelectedCustomer.Username;
            this.Password = this.SelectedCustomer.Password;
            this.Contact = this.SelectedCustomer.Contact;
            this.BankCardNumber = this.SelectedCustomer.BankCardNumber;
            this.SelectedPaymentMethod = this.SelectedCustomer.PrefferedPayment == PaymentMethod.Cash
                                             ? this.AvailablePaymentMethod[0]
                                             : this.AvailablePaymentMethod[1];
        }

        /// <summary>
        /// Clears all fields
        /// </summary>
        private void ClearAllFields()
        {
            this.Name = string.Empty;
            this.Surname = string.Empty;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.Contact = string.Empty;
            this.BankCardNumber = string.Empty;
            this.SelectedPaymentMethod = this.AvailablePaymentMethod[0];
        }
    }
}
